package com.bjpowernode.singleton.attack;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @ClassName Singleton08
 *
 * @Author Cat
 */
public enum SingletonAttack08 {

    INSTANCE;

    public String sayHi() {
        //java.lang.Enum
        return "hahahahahahaha..........";
    }

    public static void main(String[] args) throws Exception{
        /*SingletonAttack08 instance1 = SingletonAttack08.INSTANCE;
        System.out.println(instance1);

        Constructor<SingletonAttack08> constructor = SingletonAttack08.class.getDeclaredConstructor(String.class, int.class);
        constructor.setAccessible(true);//对私有构造器开放权限

        SingletonAttack08 newInstance1 = constructor.newInstance();
        System.out.println(newInstance1);

        SingletonAttack08 newInstance2 = constructor.newInstance();
        System.out.println(newInstance2);*/

        //序列化
        SingletonAttack08 instance1 = SingletonAttack08.INSTANCE;
        System.out.println(instance1);

        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("SerSingleton.obj"));
        oos.writeObject(instance1);
        oos.flush();
        oos.close();

        //反序列化
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("SerSingleton.obj"));
        SingletonAttack08 rs01 = (SingletonAttack08)ois.readObject();
        ois.close();
        System.out.println(rs01 == instance1);
    }
}